﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Biz;
using System.Collections;


namespace control.Dal
{
    class DalPhieuXuatHang
    {
        internal static int Insert(control.Biz.BizPhieuXuatHang bizPhieuXuatHang)
        {
            try
            {
                Sach_WebDataContext db = DalCommon.SachDB;
                PhieuXuatHang pxh = new PhieuXuatHang()
                {
                    MaPX = bizPhieuXuatHang.Ma,
                    NgayXuat = bizPhieuXuatHang.NgayXuat,
                    IDKhachHang = bizPhieuXuatHang.KhachHang.IDKhachHang,
                };
                db.PhieuXuatHangs.InsertOnSubmit(pxh);
                db.SubmitChanges();
                foreach (BizChiTietPhieuXuat item in bizPhieuXuatHang.Items)
                {
                    ChiTietPhieuXuat ct = new ChiTietPhieuXuat()
                    {
                        IDPhieuXuat = pxh.IDPhieuXuat,
                        SoLuong = item.SoLuong,
                        IDSach = item.IdSach,
                    };
                    db.ChiTietPhieuXuats.InsertOnSubmit(ct);
                    db.SubmitChanges();
                    if (ct.ID != 0)
                    {
                        BizTonKho tonkho = new BizTonKho()
                        {
                            IDSach = item.IdSach,
                            ThoiGian = (DateTime)pxh.NgayXuat,
                            SoLuong = DalTonKho.GetLastByIDSach(item.IdSach, (DateTime)pxh.NgayXuat) - item.SoLuong
                        };
                        tonkho.Insert();
                    }
                }
                return pxh.IDPhieuXuat;
            }
            catch { return -1; }
        }

        public static double GetTonKhoOfSach(int ID)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var tks = from t in db.TonKhos
                      where t.IDSach == ID
                      orderby t.ThoiGian descending
                      select new BizTonKho()
                      {
                          ID=t.ID,
                          IDSach=t.IDSach,
                          SoLuong=(int)t.SoLuong,
                          ThoiGian=(DateTime)t.ThoiGian
                      };
            double resuilt = 0;
            
            try { resuilt = tks.FirstOrDefault<BizTonKho>().SoLuong; }
            catch { }
            return resuilt;
         
        }

        internal static List<BizPhieuXuatHang> GetAll()
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var pxs = from px in db.PhieuXuatHangs
                      select new BizPhieuXuatHang()
                      {
                          ID = px.IDPhieuXuat,
                          Ma = px.MaPX,
                          KhachHang = BizKhachHang.GetByID(px.IDKhachHang),
                          NgayXuat = (DateTime)px.NgayXuat,
                          Items = GetListByIDPhieu(px.IDPhieuXuat)
                      };
            return pxs.ToList<BizPhieuXuatHang>();
        }

        private static List<BizChiTietPhieuXuat> GetListByIDPhieu(int p)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var items = from item in db.ChiTietPhieuXuats
                        where item.IDPhieuXuat == p
                        select new BizChiTietPhieuXuat()
                        {
                            Sach = BizSach.GetSachByID((int)item.IDSach),
                            SoLuong = (int)item.SoLuong,
                        };
            return items.ToList<BizChiTietPhieuXuat>();
        }

        internal static void Delete(BizPhieuXuatHang bizPhieuXuatHang)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            foreach (BizChiTietPhieuXuat item in bizPhieuXuatHang.Items)
            {
                //ChiTietPhieuXuat ct = db.ChiTietPhieuXuats.Single(c => (c.IDPhieuXuat == bizPhieuXuatHang.ID && c.IDSach == item.Sach.IdSach));
                //db.ChiTietPhieuXuats.DeleteOnSubmit(ct);
                //db.SubmitChanges();
                DalTonKho.DeleteByIDSach_TG(item.Sach.IdSach, bizPhieuXuatHang.NgayXuat);
            }
            PhieuXuatHang px = db.PhieuXuatHangs.Single(p => p.IDPhieuXuat == bizPhieuXuatHang.ID);
            IEnumerable<ChiTietPhieuXuat> cts = px.ChiTietPhieuXuats;
            db.ChiTietPhieuXuats.DeleteAllOnSubmit(cts);
            db.SubmitChanges();
            db.PhieuXuatHangs.DeleteOnSubmit(px);
            db.SubmitChanges();
        }

        internal static List<BizPhieuXuatHang> Find(string p, DateTime FromDateTime, DateTime ToDateTime)
        {
            Sach_WebDataContext db = DalCommon.SachDB;
            var pxs = from px in db.PhieuXuatHangs
                      where (px.MaPX.Contains(p)||px.KhachHang.TenKH.Contains(p)||px.KhachHang.TenDN.Contains(p))
                      &&((DateTime)px.NgayXuat).CompareTo(FromDateTime)>=0
                      &&((DateTime)px.NgayXuat).CompareTo(ToDateTime)<=0
                      select new BizPhieuXuatHang()
                      {
                          ID = px.IDPhieuXuat,
                          Ma = px.MaPX,
                          KhachHang = BizKhachHang.GetByID(px.IDKhachHang),
                          NgayXuat = (DateTime)px.NgayXuat,
                          Items = GetListByIDPhieu(px.IDPhieuXuat)
                      };
            return pxs.ToList<BizPhieuXuatHang>();


        }
    }
}
